package com.nvshen.zhangmeiying.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Environment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wxc
 * @version 创建时间：2014-6-10 下午2:54:50
 *          类说明
 */
public class Utils {
    public static boolean hasGingerbread() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
    }

    public static boolean hasFroyo() {
        // Can use static final constants like FROYO, declared in later versions
        // of the OS since they are inlined at compile time. This is guaranteed
        // behavior.
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
    }

    /**
     * 是否为有效手机号
     *
     * @param str
     * @return
     */
    public static boolean isCellphone(String str) {
        Pattern pattern = Pattern.compile("1[0-9]{10}");
        Matcher matcher = pattern.matcher(str);
        if (matcher.matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否为有效邮箱
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);

        return m.matches();
    }

    /**
     * @param str
     * @return
     */
    public static boolean isProperUserSecret(String str) {
        if (str.length() >= 6 && str.length() <= 16) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * i是否大于某个时间
     *
     * @param endDate 格式 年-月-日
     * @return
     */
    public static boolean isBiggerThanDate(String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(endDate);
            Date nowDate = new Date(System.currentTimeMillis());
            Logger.d(nowDate.getTime() + "", date.getTime() + "");
            if (nowDate.getTime() < date.getTime()) {
                return false;
            } else {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断身份证格式
     *
     * @param idCard
     * @return
     */
    public static boolean isIDcard(String idCard) {
        if (idCard.length() == 18) {
            String str = idCard;
            int[] num = new int[17];
            for (int i = 0; i < str.length() - 1; i++) {
                if (Character.isDigit(str.charAt(i))) {
                    num[i] = Integer.parseInt(str.substring(i, i + 1));
                } else {
                    return false;
                }
            }
            char finalChar = str.charAt(17);
            int yushu = (num[0] * 7 + num[1] * 9 + num[2] * 10 + num[3] * 5
                    + num[4] * 8 + num[5] * 4 + num[6] * 2 + num[7] * 1
                    + num[8] * 6 + num[9] * 3 + num[10] * 7 + num[11] * 9
                    + num[12] * 10 + num[13] * 5 + num[14] * 8 + num[15] * 4 + num[16] * 2) % 11;
            char result = 'a';
            switch (yushu) {
                case 0:
                    result = '1';
                    break;
                case 1:
                    result = '0';
                    break;
                case 2:
                    result = 'X';
                    break;
                case 3:
                    result = '9';
                    break;
                case 4:
                    result = '8';
                    break;
                case 5:
                    result = '7';
                    break;
                case 6:
                    result = '6';
                    break;
                case 7:
                    result = '5';
                    break;
                case 8:
                    result = '4';
                    break;
                case 9:
                    result = '3';
                    break;
                case 10:
                    result = '2';
                    break;
                default:
                    break;
            }
            if (result != finalChar) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

    /**
     * 是否接入网络
     *
     * @param context
     * @return
     */
    public static boolean isConnectingToInternet(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }

        }
        return false;
    }

    /**
     * 获取文件名称
     *
     * @param pathandname
     * @return
     */
    public static String getFileNameFromPath(String pathandname) {
        int start = pathandname.lastIndexOf("/");
        return pathandname.substring(start + 1, pathandname.length());

    }

    /**
     * 获取缓存路径~
     *
     * @return
     */
    public static String getCacheRootPath(){
        return Environment.getExternalStorageDirectory() + "/perfect/.nvshen/";
    }

    /**
     * 密码校验
     * @param password
     * @return
     */
    public static boolean isProperPassword(String password){
        if(password.length() < 6){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 手机或邮箱检验
     * @param info
     * @return
     */
    public static boolean isProperCellOrMail(String info){
        if(info.length() < 6){
            return false;
        }else{
            return true;
        }
    }

}

